# Maintainer: Junjie Mao <eternal.n08@gmail.com>

_bootstrap_flexdll=1

_realname=ocaml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-compiler-libs"
         $( (( _bootstrap_flexdll )) && echo "${MINGW_PACKAGE_PREFIX}-flexdll-bootstrap"))
pkgver=4.14.0
pkgrel=1
_flexdll_ver=0.42
pkgdesc="An industrial strength programming language supporting functional, imperative and object-oriented styles (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
url='https://ocaml.org/'
license=('Q Public Licence 1.0' 'GPL2')
makedepends=("${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
optdepends=("${MINGW_PACKAGE_PREFIX}-ncurses: advanced ncurses features"
            "${MINGW_PACKAGE_PREFIX}-tk: advanced tk features")
source=("https://caml.inria.fr/distrib/ocaml-${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        "https://github.com/alainfrisch/flexdll/archive/${_flexdll_ver}.tar.gz"
        "0001-fix-misc-h.patch"
        "0002-support-clang.patch")
sha256sums=('36abd8cca53ff593d5e7cd8b98eee2f1f36bd49aaf6ff26dc4c4dd21d861ac2b'
            'a2dce0a0d2f2c5c9f52f65e281db7405f51ff22dc71cdbf494eb241c8a4bdf0f'
            '24d20cc5fc9c0f70e5c54c2378f0be612ae474425dd5918b321469fb887eb358'
            '564f964efe14100d88e42895dec4e9f6faa0952f0083de175bbf051245154237')
options=('!makeflags' '!emptydirs' 'staticlibs')
install=ocaml-${MSYSTEM}.install

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cp -r "${srcdir}/flexdll-${_flexdll_ver}/"* flexdll

  patch -p1 -i "${srcdir}/0001-fix-misc-h.patch"
  patch -p1 -i "${srcdir}/0002-support-clang.patch"

  export _cc_ver=$(${CC} -dumpversion)
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    export _cc_lib_dir=${MINGW_PREFIX}/lib/clang/${_cc_ver}/windows/
  else
    export _cc_lib_dir=${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${_cc_ver}/
  fi

  #Necessary for ocaml to find ws2_32
  export SEDTEXT="s:    FLEXLINK_CMD=\"../boot/ocamlruns\$(EXE) ../boot/flexlink.byte\$(EXE)\":    FLEXLINK_CMD=\"../boot/ocamlruns\$(EXE) ../boot/flexlink.byte\$(EXE) -L${MINGW_PREFIX}/lib -L${_cc_lib_dir}\":g"
  sed -i "${SEDTEXT}" Makefile

  SEDTEXT="s:CAMLOPT=\$(OCAMLRUN) ./ocamlopt\$(EXE) -g -nostdlib -I stdlib -I otherlibs/dynlink:CAMLOPT=\$(OCAMLRUN) ./ocamlopt\$(EXE) -g -nostdlib -I stdlib -I otherlibs/dynlink -cclib \"-L${MINGW_PREFIX}/lib -L${_cc_lib_dir}\":g"
  sed -i "${SEDTEXT}" Makefile

  SEDTEXT="s:CAMLOPT_CMD = \$(FLEXLINK_ENV) \$(CAMLOPT):CAMLOPT_CMD = \$(FLEXLINK_ENV) \$(CAMLOPT) -cclib \"-L${MINGW_PREFIX}/lib -L${_cc_lib_dir}\":g"
  sed -i "${SEDTEXT}" Makefile.common

  SEDTEXT="s:OCAMLOPT_CMD = \$(FLEXLINK_ENV) \$(OCAMLOPT):OCAMLOPT_CMD = \$(FLEXLINK_ENV) \$(OCAMLOPT) -cclib \"-L${MINGW_PREFIX}/lib -L${_cc_lib_dir}\":g"
  sed -i "${SEDTEXT}" Makefile.common

  SEDTEXT="s:WIN32_LIBS=\$(call SYSLIB,ws2_32) \$(call SYSLIB,advapi32):WIN32_LIBS=\$(call SYSLIB,ws2_32) \$(call SYSLIB,advapi32) -L${MINGW_PREFIX}/lib -L${_cc_lib_dir}:g"
  sed -i "${SEDTEXT}" otherlibs/win32unix/Makefile

  SEDTEXT="s:MKLIB=\$(OCAMLRUN) \$(ROOTDIR)/tools/ocamlmklib\$(EXE):MKLIB=\$(OCAMLRUN) \$(ROOTDIR)/tools/ocamlmklib\$(EXE) -L${MINGW_PREFIX}/lib -L${_cc_lib_dir}:g"
  sed -i "${SEDTEXT}" otherlibs/systhreads/Makefile

  SEDTEXT="s:LDOPTS ?=:LDOPTS = -L${MINGW_PREFIX}/lib -L${_cc_lib_dir}:g"
  sed -i "${SEDTEXT}" otherlibs/Makefile.otherlibs.common

  SEDTEXT="s:-safe-string -strict-sequence -strict-formats -bin-annot -principal:-safe-string -strict-sequence -strict-formats -bin-annot -principal -cclib \"-L${MINGW_PREFIX}/lib -L${_cc_lib_dir}\":g"
  sed -i "${SEDTEXT}" ocamldoc/Makefile

  SEDTEXT="s:LINKFLAGS = -cclib \"-link \$(RES)\":LINKFLAGS = -cclib \"-link \$(RES) -L${MINGW_PREFIX}/lib -L${_cc_lib_dir}\":g"
  sed -i "${SEDTEXT}" flexdll/Makefile
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  export BFD_LIB_DIR="${MINGW_PREFIX}/lib/binutils/"
  export BFD_INCLUDE_DIR="${MINGW_PREFIX}/include/binutils/"
  export LDFLAGS="-L${MINGW_PREFIX}/lib -L${_cc_lib_dir}"

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --mandir=${MINGW_PREFIX}/share/man \
    --with-flexdll \
    $([[ ${CARCH} == i686 ]] && echo "--enable-imprecise-c99-float-ops") \
    --disable-force-safe-string

  SEDTEXT="s:BYTECCLIBS=-lws2_32 -lversion:BYTECCLIBS=-L${MINGW_PREFIX}/lib -L${_cc_lib_dir} -lws2_32 -lversion:g"
  sed -i "${SEDTEXT}" Makefile.config

  SEDTEXT="s:PTHREAD_LINK=:PTHREAD_LINK=-L${MINGW_PREFIX}/lib -L${_cc_lib_dir}:g"
  sed -i "${SEDTEXT}" Makefile.config

  make world.opt
}

package_ocaml() {
  cd "${srcdir}/${_realname}-${pkgver}"
  make DESTDIR="${pkgdir}" install

  # install license
  install -m755 -d "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -m644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"

  # remove compiler and flex libs
  rm -rf "${pkgdir}${MINGW_PREFIX}/lib/ocaml/compiler-libs"
  mkdir -p "${srcdir}"/install-flexdll${MINGW_PREFIX}/lib/ocaml
  mkdir -p "${srcdir}"/install-flexdll${MINGW_PREFIX}/bin
  mv "${pkgdir}"${MINGW_PREFIX}/lib/ocaml/flexdll "${srcdir}"/install-flexdll${MINGW_PREFIX}/lib/ocaml/flexdll
  mv "${pkgdir}"${MINGW_PREFIX}/bin/flexlink*.exe "${srcdir}"/install-flexdll${MINGW_PREFIX}/bin/
}

package_ocaml-compiler-libs() {
  pkgdesc="Several modules used internally by the OCaml compiler (mingw-w64)"
  license=('Q Public Licence 1.0')
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/${_realname}-${pkgver}"
  make DESTDIR="${pkgdir}" install
  # Remove non-compiler-libs
  rm -rf   "${pkgdir}${MINGW_PREFIX}/bin"  "${pkgdir}${MINGW_PREFIX}/lib/ocaml/caml" \
     "${pkgdir}${MINGW_PREFIX}/lib/ocaml/ocamldoc" "${pkgdir}${MINGW_PREFIX}/lib/ocaml/stublibs" \
     "${pkgdir}${MINGW_PREFIX}/lib/ocaml/threads" "${pkgdir}${MINGW_PREFIX}/share"
  find "${pkgdir}${MINGW_PREFIX}/lib/ocaml/" -maxdepth 1 -type f -delete

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-compiler-libs"
  install -m644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-compiler-libs/"

  # remove flexdll libs
  rm -rf "${pkgdir}${MINGW_PREFIX}/lib/ocaml/flexdll"
}

package_flexdll-bootstrap() {
  pkgdesc="An implementation of a dlopen-like API for Windows (For bootstrapping) (mingw-w64)"

  mv "${srcdir}"/install-flexdll/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
